﻿using MyDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rtz2Xlsx
{
    public partial class FormManageSettingsPlans : Form
    {
        public FormManageSettingsPlans()
        {
            InitializeComponent();
        }

        private void dgvMngPlns_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                                                e.RowBounds.Location.Y,
                                                dgv.RowHeadersWidth - 4,
                                                e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                                    dgv.RowHeadersDefaultCellStyle.Font,
                                    rectangle,
                                    dgv.RowHeadersDefaultCellStyle.ForeColor,
                                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        /// <summary>
        /// 在DataGridView中添加行
        /// </summary>
        /// <param name="strArr">长度为4的字符串数组</param>
        private void MyDataGridViewAddRow(string[] strArr)
        {
            DataGridViewRow dr = new DataGridViewRow();
            DataGridViewCell cell1 = new DataGridViewTextBoxCell();//文件名
            DataGridViewCell cell2 = new DataGridViewTextBoxCell();//创建时间
            DataGridViewCell cell3 = new DataGridViewTextBoxCell();//修改时间

            cell1.Value = strArr[0];
            cell2.Value = strArr[1];
            cell3.Value = strArr[2];

            dr.Cells.Add(cell1);
            dr.Cells.Add(cell2);
            dr.Cells.Add(cell3);

            dgvMngPlns.Rows.Add(dr);
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            List<int> selectedRowsList = new List<int>();
            foreach (DataGridViewCell item in dgvMngPlns.SelectedCells)
            {
                selectedRowsList.Add(item.RowIndex);
            }
            selectedRowsList = selectedRowsList.Distinct().ToList();

            if (!selectedRowsList.Contains(0))  //如果选中项不包含第一项，则执行删除指令
            {
                for (int i = 0; i < selectedRowsList.Count; i++)
                {
                    //在XML文件中删除相关节点
                    FormMain.xpath_ToPlan = FormMain.xpath_Plan + "[@" + XmlSettings.planAttribute_1 + "='" + dgvMngPlns.Rows[selectedRowsList[i]].Cells[0].Value.ToString() + "']";
                    FormMain.xML_Commons.DeleteNodes(FormMain.xpath_ToPlan);
                    dgvMngPlns.Rows.RemoveAt(selectedRowsList[i]);
                }
            }
            else
            {
                MessageBox.Show("不可删除第一项！");
            }
        }

        private void FormManageSettingsPlans_Load(object sender, EventArgs e)
        {
            List<string[]> AttributesList = FormMain.xML_Commons.AllSelectedNodesSelectedAttributes(FormMain.xpath_Plan, XmlSettings.Attributes_Parent);
            for (int i = 0; i < AttributesList.Count; i++)
            {
                MyDataGridViewAddRow(AttributesList[i]);
            }
        }

        private void dgvMngPlns_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            FormSettings.UpdateComboBoxPlanSelection_FromFormPlanManagement();
            FormMain.UpdateComboBoxSettingsPlans_FromOtherForms();
        }
    }
}
